﻿using System;
using masterview.Code;
using masterview.Code.db;

namespace masterview
{
    public partial class WebForm4 : System.Web.UI.Page
    {
        public ServerSideMessage ssm = ServerSideMessage.GetInstance();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (GridView1.SelectedIndex==-1 || GridView2.SelectedIndex==-1)
                {
                    ssm.IsActive = true;
                    ssm.Message = "Please select service and database.";
                    ssm.Title = "Make your choice...";
                    ssm.NotifyType = ServerSideMessage.MessageType.Warning;
                    return;
                }
                var serviceId = GridView1.SelectedValue.ToString();
                var connestionStringId = GridView2.SelectedValue.ToString();

                //gets connection string from local database
                var db = new Database();
                var connestionString = db.GetConnectionStringById(connestionStringId);

                //sets connection string for selected service
                var result = db.SetConnectionStringForService(serviceId, connestionString);
                if (result)
                {
                    ssm.IsActive = true;
                    ssm.Message = "Service was configured";
                    ssm.Title = "Ready!";
                    ssm.NotifyType = ServerSideMessage.MessageType.Success;
                    return;
                }
                ssm.IsActive = true;
                ssm.Message = "Something wrong!";
                ssm.Title = "";
                ssm.NotifyType = ServerSideMessage.MessageType.Error;
                return;
            }
            catch (NullReferenceException ex)
            {
                ssm.IsActive = true;
                ssm.Message = "Please select service and database.";
                ssm.Title = "Make your choice...";
                ssm.NotifyType = ServerSideMessage.MessageType.Warning;
                return;
            }
        }
    }
}